package com.otp.controller;

import com.otp.pojo.dto.QuestionDto;
import com.otp.pojo.entity.Question;
import com.otp.pojo.request.question.*;
import com.otp.util.ResponseMessage;
import com.otp.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @GetMapping
    public ResponseMessage<List<QuestionDto>> list() {
        return ResponseMessage.success(questionService.list()
                .stream()
                .map(Question::convertToDto)
                .toList());
    }

    @GetMapping("/status/{status}")
    public ResponseMessage<List<QuestionDto>> listByStatus(@PathVariable String status) {
        return ResponseMessage.success(questionService.listByStatus(status)
                .stream()
                .map(Question::convertToDto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<QuestionDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(questionService.getById(id)
                .convertToDto());
    }

    @PostMapping
    public ResponseMessage<QuestionDto> create(@RequestBody QuestionCreateRequest request) {
        return ResponseMessage.success(questionService.create(request)
                .convertToDto());
    }

    @PutMapping
    public ResponseMessage<QuestionDto> update(@RequestBody QuestionUpdateRequest request) {
        return ResponseMessage.success(questionService.update(request)
                .convertToDto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        questionService.delete(id);
        return ResponseMessage.success(null);
    }

    @PostMapping("/publish/{id}")
    public ResponseMessage<QuestionDto> publish(@PathVariable Long id) {
        return ResponseMessage.success(questionService.publish(id)
                .convertToDto());
    }
}